/*
 * ExpTreenode.java
 */

package complexchaos;

import java.util.*;
import java.lang.Object;

/**
 * The treenode class used by ExpFormula.java
 *
 * @author Don Spickler
 * @see ExpFormula
 */
public class ExpTreenode implements Cloneable 
{
	public String op_str; 
	public ComplexNumber num;
	public ExpTreenode left;
	public ExpTreenode right;
	
	public ExpTreenode(final String s)
	{
		this(s, null, null, null);
	}

	public ExpTreenode(final ComplexNumber n)
	{
		this("#", n, null, null);
	}

	public ExpTreenode(final String s, final ComplexNumber n, final ExpTreenode l, final ExpTreenode r)
	{
		op_str = s;
		num = n;
		left = l;
		right = r;
	}
	
	public synchronized ExpTreenode clone()
	{
		if (num != null)
			return new ExpTreenode(op_str, num.clone(), (left != null ? (ExpTreenode)left.clone() : null), (right != null ? (ExpTreenode)right.clone() : null));
		else
			return new ExpTreenode(op_str, null, (left != null ? (ExpTreenode)left.clone() : null), (right != null ? (ExpTreenode)right.clone() : null));
	}	
	
	public boolean equals(ExpTreenode t)
	{
		if (t == null) return false;
		
		boolean leftEqual = false;
		boolean rightEqual = false;
		
		if ((left != null) && (t.left != null))
			leftEqual = left.equals(t.left);
		else if ((left == null) && (t.left == null))
			leftEqual = true;

		if ((right != null) && (t.right != null))
			rightEqual = right.equals(t.right);
		else if ((right == null) && (t.right == null))
			rightEqual = true;
		
		return leftEqual && rightEqual && op_str.equalsIgnoreCase(t.op_str) && num.equals(t.num);
	}
	

//	public synchronized boolean dataEqual(ArrayList List1, ArrayList List2)
//	{
//		if (List1 == null)
//			return List2 == null;
//		
//		if (List1.size() != List2.size())
//			return false;
//
//		for (int i = 0; i < List1.size(); i++)
//		{
//			if (List1.get(i) instanceof Long)
//			{
//				if (List2.get(i) instanceof Long)
//				{
//					if (((Long)List1.get(i)).longValue() != ((Long)List2.get(i)).longValue())
//						return false;
//				}
//				else
//					return false;
//			}
//
//			if (List1.get(i) instanceof String)
//			{
//				if (List2.get(i) instanceof String)
//				{
//					if (!((String)List1.get(i)).equalsIgnoreCase(((String)List2.get(i))))
//						return false;
//				}
//				else
//					return false;
//			}
//
//			if (List1.get(i) instanceof ArrayList)
//			{
//				if (List2.get(i) instanceof ArrayList)
//				{
//					if (!dataEqual((ArrayList)List1.get(i), (ArrayList)List2.get(i)))
//						return false;
//				}
//				else
//					return false;
//			}
//		}
//		
//		return true;
//	}

}